const db=uniCloud.database();
const cmd = db.command;
module.exports = async (obj) => {
	let user_order_success = true;
	let { data = {} } = obj;
	let {
		order_no,
		out_trade_no,
		total_fee
	} = data; // uni-pay-orders 表内的数据均可获取到
	let orderRes = await db.collection("wanshop-mall-order").where({order_no}).get();
	let carsList = orderRes.data[0].carsList
	let ids = carsList.map(item=>{
		return item.goodsid
	})
	
	// 根据id获取对应商品
	let goodsRes = await db.collection("wanshop-mall-goods").where({
		_id:cmd.in(ids)
	}).get();
	let totalPrice = 0;
	goodsRes.data.forEach(item=>{
	 let idx = carsList.findIndex(item2 =>{
			return item._id == item2.goodsid		})
		totalPrice += carsList[idx].numvalue * item.price 	
			
	})
	
	
	if(totalPrice == total_fee){
		
		 db.collection("wanshop-mall-order").where({order_no}).update({
			status:1
		})
	}else{
		 db.collection("wanshop-mall-order").where({order_no}).update({
			status:-2,
			totalPrice:totalPrice
		})
		user_order_success = false
	}
	let res = await db.collection("wanshop-mall-order").where({order_no}).get()
	
	console.log(res.data[0].status);
	
	return user_order_success;
};